clear all
set more off
set matsize 2000

*input folder path      
global filepath "D:\Dropbox\NREGA Aggregate\shared NREGA lights\replication files"

	  
cd "$filepath\raw data and cleaning"

**************************
*** Call Data from RBI ***
**************************

use "raw deposits.dta", clear

****************************************************************
*** Cleana and Adjust (Fiscal to Calendar Year) Deposit Data ***
****************************************************************

gen deposit_2004_q1="."
gen deposit_2016_q4="."

gen credit_2004_q1="."
gen credit_2016_q4="."

*Adjust quarter
forvalues i=2004/2016{
local j=`i'-1
rename deposit_`i'_q4 dep`i'03
replace dep`i'03 = subinstr(dep`i'03,",","",.)
destring dep`i'03, replace

rename deposit_`i'_q3 dep`i'02
replace dep`i'02 = subinstr(dep`i'02,",","",.)
destring dep`i'02, replace

rename deposit_`i'_q2 dep`i'01
replace dep`i'01 = subinstr(dep`i'01,",","",.)
destring dep`i'01, replace

rename deposit_`i'_q1 dep`j'04
replace dep`j'04 = subinstr(dep`j'04,",","",.)
destring dep`j'04, replace

rename credit_`i'_q4 cred`i'03
replace cred`i'03 = subinstr(cred`i'03,",","",.)
destring cred`i'03, replace

rename credit_`i'_q3 cred`i'02
replace cred`i'02 = subinstr(cred`i'02,",","",.)
destring cred`i'02, replace

rename credit_`i'_q2 cred`i'01
replace cred`i'01 = subinstr(cred`i'01,",","",.)
destring cred`i'01, replace

rename credit_`i'_q1 cred`j'04
replace cred`j'04 = subinstr(cred`j'04,",","",.)
destring cred`j'04, replace
}

order state distname dep*

*pre-period growth rate of deposits
gen ddep04_05 = ((dep200504)/(dep200401))^(1/8)-1

*pre-period mean deposits
egen mean_predep0 = rowmean(dep200401 dep200402 dep200403 dep200404 dep200501 dep200502 dep200503 dep200504)
gen mean_predep = mean_predep0/100



*******************************
*** Combine with Other Data ***
*******************************
*drop missing and duplicates
drop if distname==""
drop if dep201602==.
duplicates tag distname, gen(dup)
drop if dup>0

merge 1:1 distname using "district census code.dta", nogen
order census_no
drop if census_no==.


merge 1:1 census_no using "lights_wide.dta", keepusing(sno nr06 nr07 nr08 rggvy wage outputwage state_frac lat lon)


***************
*** Reshape ***
***************
reshape long dep cred, i(census_no) j(yrqtr)


*********************************
*** NREGS and Other Variables ***
*********************************
 
gen nregs=0
replace nregs=1 if nr06==1 & yrqtr>=200601
replace nregs=1 if nr07==1 & yrqtr>=200701
replace nregs=1 if nr08==1 & yrqtr>=200801


encode state, gen(st)

gen year=.
forvalues i=2004/2013{
replace year=`i' if yrqtr>=`i'01 & yrqtr<=`i'04
}


*************
*** Panel ***
*************
drop if sno==.

drop if yrqtr<200400
drop if yrqtr>201304

*check balance
bysort census_no: egen wi_dis_obs = count(dep)
sum wi_dis_obs





****************************************
*** Base Sample, Removing Singletons ***
****************************************
*Base sample (no singletons)
reghdfe dep nregs, absorb(dis_fe=i.sno state_by_yqtr=i.yrqtr#i.st) cluster(sno) resid
predict e, residuals
gen bs=0
replace bs=1 if e!=.
reghdfe dep nregs if bs==1, absorb(sno yrqtr#st) cluster(sno)
drop if bs!=1



****************************
*** Standardize Deposits ***
****************************
egen std_dep = std(dep)

****************
*** Cleaning ***
****************

drop cred dup  _reghdfe_resid dis_fe  state_by_yqtr e bs mean_predep0 wi_dis_obs _merge

rename state state_name
rename distname district_name

order sno census_no state_name district_name yrqtr year nregs nr06-nr08 dep std_dep rggvy wage outputwage state_frac st  mean_predep ddep04_05

label var sno "district id"
label var yrqtr "year-quarter"
label var year "year"
label var nr06 "wave 1"
label var nr07 "wave 2"
label var nr08 "wave 3"
label var nregs "NREGS"
label var state_name "state name"
label var district_name "district name"
label var census_no "district census id"
label var dep "district deposits from RBI"
label var std_dep "Std. Deposits"
label var state_frac "sc/st frac."
label var wage "agr. wage"
label var outputwage "output per agr. worker"
label var rggvy "RGGVY, 10th plan"
label var st "state encode"
label var mean_predep "mean level of deposits, 2004-2005"
label var ddep04_05 "mean growth rate deposits, 2004-2005"




save "$filepath\district deposit panel.dta", replace


